/**
 * виджет бегущей строки
 */

;(function($){

  $.widget("ui.crawlLine", {
    options : {speed: 2, crawElement: "div", textElement: "p", hoverClass: "viewText"},
  // конструктор
    _create : function()
              {
                $.extend(this, {
                  craw: this.element.find(this.options.crawElement),
                  text: this.element.find(this.options.textElement)
                });
                var self = $.extend(this, {
                  _clone    : this.text.clone(),
                  _position : this.craw.map(function(){with($(this))return{p:css("position"),w:width()};}),
                  _wspace   : this.text.map(function(){return $(this).css("whiteSpace");}),
                  _textWidth: 0,
                  _onEnter  : function(){self.craw.stop();$(this).addClass(self.options.hoverClass);},
                  _onLeave  : function()
                              {
                                $(this).removeClass(self.options.hoverClass);
                                self._animate((self._textWidth + parseInt(self.craw.css("left"))) / self._textWidth);
                              },
                  _onWheel  : function(e, d)
                              {
                                var marginScroll = parseInt(self.craw.css("left"));
                                marginScroll     = d < 0 ? marginScroll - 20 : Math.min( 0, marginScroll + 20 );
                                self.craw.animate({left: marginScroll}, {queue: false, duration: 100, easing: "linear"});
                                return false;
                              }
                });
                this.craw.css("position", "relative");
                this.text.each(function(){self._textWidth+=$(this).css("whiteSpace","nowrap").outerWidth(true);});
                this.craw.append(this._clone).width(this._textWidth << 1);
                this._animate();
                this.element.hover(this._onEnter, this._onLeave).wheel(this._onWheel);
              },
  // деструктор
    destroy : function()
              {
                this.craw.
                  unbind("wheel", this._onWheel).
                  unbind("leave", this._onLeave).
                  unbind("enter", this._onEnter).
                  stop();
                this._clone.remove();
                var self = this;
                this.text.each(function(i){$(this).css("whiteSpace",self._wspace[i]);});
                this.craw.each(function(i){$(this).css("position",self._position[i].p).width(self._position[i].w);});
              },
  // анимация
    _animate: function(k)
              {
                var self = this;
                this.craw.animate({left: -this._textWidth}, {
                  queue   : false,
                  duration: this._textWidth * 50 * (k || 1) / this.options.speed,
                  easing  : "linear",
                  complete: function(){$(this).css("left",0);self._animate();}
                });
              }
  });

})(jQuery);
